import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/themes/appTheme.dart';
import 'package:toggle_switch/toggle_switch.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:universal_html/html.dart' as uHtml;
import 'package:pointer_interceptor/pointer_interceptor.dart';

class TeacherAppDrawer extends StatelessWidget {
  TeacherAppDrawer({Key? key}) : super(key: key);
  final UserController _userController = Get.find();
  final box = Hive.box('appconfig');

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        primary: false,
        child: Column(
          children: <Widget>[
            SizedBox(height: 50),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/home',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.home,
                  color: Get.currentRoute == '/teacher/home'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Home".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/home'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.offAllNamed('/teacher/home');
                  } else {
                    Get.offAllNamed('/teacher/home');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/students',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.people_alt_rounded,
                  color: Get.currentRoute == '/teacher/students'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Students".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/students'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/students');
                  } else {
                    Get.offAllNamed('/teacher/students');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/corematerials'||
                    Get.currentRoute == '/teacher/themedbooks',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.book_online_rounded,
                  color: Get.currentRoute == '/teacher/corematerials' ||
                          Get.currentRoute == '/teacher/themedbooks'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Core Materials".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/corematerials'||
                                Get.currentRoute == '/teacher/themedbooks'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  String programId = box
                      .get(CacheManagerKey['currentclassProgramId'].toString());
                  var program = Constants.nanhaiImmersionSubProgramsDropdown
                      .where((element) => element.value == programId)
                      .toList();

                  if (GetPlatform.isWeb) {
                    if (program.isNotEmpty) {
                      Get.toNamed('/teacher/corematerials');
                    } else {
                      Get.toNamed('/teacher/themedbooks');
                    }
                  } else {
                    if (program.isNotEmpty) {
                      Get.offAllNamed('/teacher/corematerials');
                    } else {
                      Get.offAllNamed('/teacher/themedbooks');
                    }
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/lessonplans',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.play_lesson_rounded,
                  color: Get.currentRoute == '/teacher/lessonplans'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Lesson Plans".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/lessonplans'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/lessonplans');
                  } else {
                    Get.offAllNamed('/teacher/lessonplans');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/library',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.local_library,
                  color: Get.currentRoute == '/teacher/library'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Library".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/library'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/library');
                  } else {
                    Get.offAllNamed('/teacher/library');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/homework',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.home_work_rounded,
                  color: Get.currentRoute == '/teacher/homework'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Homework".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/homework'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/homework');
                  } else {
                    Get.offAllNamed('/teacher/homework');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/assessments',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  //Icons.text_snippet_rounded,
                  Icons.assessment_rounded,
                  color: Get.currentRoute == '/teacher/assessments'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Assessments".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/assessments'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/assessments');
                  } else {
                    Get.offAllNamed('/teacher/assessments');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/evaluation',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.high_quality_rounded,
                  color: Get.currentRoute == '/teacher/evaluation'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Evaluation".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/evaluation'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    Get.toNamed('/teacher/evaluation');
                  } else {
                    Get.offAllNamed('/teacher/evaluation');
                  }
                },
              ),
            ),
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            PointerInterceptor(
              child: ListTile(
                selected: Get.currentRoute == '/teacher/reports',
                selectedTileColor: Get.isDarkMode
                    ? Get.theme.primaryColor
                    : Get.theme.primaryColorDark,
                leading: Icon(
                  Icons.bar_chart_rounded,
                  color: Get.currentRoute == '/teacher/reports'
                      ? AppTheme.colors.drawerItemColor
                      : AppTheme.colors.drawerItemSelectedColor,
                  size: 30.0,
                ),
                title: Text(
                  "Reports".tr,
                  style: Theme.of(context).textTheme.displayLarge?.copyWith(
                        color: Get.currentRoute == '/teacher/reports'
                            ? AppTheme.colors.drawerItemColor
                            : AppTheme.colors.drawerItemSelectedColor,
                        fontSize: 16,
                        fontFamily: 'Montserrat',
                      ),
                ),
                onTap: () {
                  if (GetPlatform.isWeb) {
                    String _currentClassId =
                        box.get(CacheManagerKey['currentclass'].toString());
                    uHtml.document.cookie =
                        "nanhaiV4.8TeacherClassSelected=$_currentClassId;path=/";
                    Get.toNamed('/teacher/reports');
                    // launchInWebViewWithJavaScript(
                    //     '${Constants.BaseURL}icr/teacher/report');
                  } else
                    Get.offAllNamed('/teacher/reports');
                },
              ),
            ),
            // Divider(
            //   color: Theme.of(context).primaryColorLight,
            // ),
            // PointerInterceptor(
            //   child: ListTile(
            //     selected: Get.currentRoute == '/help',
            //     selectedTileColor: Get.isDarkMode
            //         ? Get.theme.primaryColor
            //         : Get.theme.primaryColorDark,
            //     leading: Icon(
            //       Icons.help_outline_rounded,
            //       color: Get.currentRoute == '/help'
            //           ? AppTheme.colors.drawerItemColor
            //           : AppTheme.colors.drawerItemSelectedColor,
            //       size: 30.0,
            //     ),
            //     title: Text(
            //       "Help Center".tr,
            //       style: Theme.of(context).textTheme.displayLarge?.copyWith(
            //             color: Get.currentRoute == '/help'
            //                 ? AppTheme.colors.drawerItemColor
            //                 : AppTheme.colors.drawerItemSelectedColor,
            //             fontSize: 16,
            //             fontFamily: 'Montserrat',
            //           ),
            //     ),
            //     onTap: () {
            //       String? local;
            //       switch (_userController.selectedLang.value) {
            //         case 'ENG':
            //           local = 'en';
            //           break;
            //         case '简体':
            //           local = 'cn';
            //           break;
            //         case '繁体':
            //           local = 'tc';
            //           break;
            //         // case '한국인':
            //         //   local = 'ko';
            //         //   break;
            //         default:
            //           local = 'en';
            //       }
            //       if (GetPlatform.isWeb || GetPlatform.isDesktop) {
            //         launchInWebViewWithJavaScript(
            //             'https://ichinesereader.com/help/$local/');
            //       } else {
            //         Get.offAllNamed('/help',
            //             arguments: Constants.NanhaiRole_Teacher);
            //       }
            //     },
            //   ),
            // ),
           
            Divider(
              color: Theme.of(context).primaryColorLight,
            ),
            SizedBox(height: 20),
            PointerInterceptor(
              child: ToggleSwitch(
                minWidth: 120.0,
                cornerRadius: 0.0,
                activeBgColors: [
                  [Color(0xff0085cc)],
                ],
                activeFgColor: Color(0xffd8ffff),
                inactiveBgColor: Color(0xff666666),
                inactiveFgColor: Color(0xffEEEEEE),
                totalSwitches: 2,
                labels: ['LIGHT', 'DARK'],
                onToggle: (index) {
                  Log.debug('switched to: $index');
                  index == 0
                      ? _userController.changeTheme(isDarkMode: false)
                      : _userController.changeTheme(isDarkMode: true);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
